package cn.shyouchi.wuguanjia.android.ui.event.checknum;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.ui.event.checkpoint.CheckPointEntity;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class AddEventCheckNumActivity extends BaseActivity {
    @BindView(R.id.topBar)
    TopBar mTopBar;

    @BindView(R.id.rv_check_num)
    RecyclerView mRvCheckNum;

    EventApi mEventApi;
    String projectId;
    String lineId;
    List<CheckPointEntity.ResultMessageBean> mPointEntities = new ArrayList<>();

    SelCheckPointAdapter mSelCheckPointAdapter;


    boolean isEdit = false;
    CheckNumEntity.ResultMessageBean entity;

    @Override
    public int initContentView() {
        return R.layout.activity_add_event_check_num;
    }

    @Override
    public void initInjector() {
        mEventApi = new EventApi(mContext);
        projectId = getIntent().getStringExtra("projectId");
        lineId = getIntent().getStringExtra("lineId");
        entity = getIntent().getParcelableExtra("checknum");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

        mSelCheckPointAdapter = new SelCheckPointAdapter(getSupportFragmentManager(), mPointEntities);
        mRvCheckNum.setLayoutManager(new LinearLayoutManager(mContext));
        mRvCheckNum.setAdapter(mSelCheckPointAdapter);


        if (entity != null) {
            isEdit = true;
            ToastUtil.showToast("编辑状态");
        }
        mEventApi.getPointList(projectId)
                .subscribeOn(Schedulers.io())
                .map(new Func1<CheckPointEntity, List<CheckPointEntity.ResultMessageBean>>() {
                    @Override
                    public List<CheckPointEntity.ResultMessageBean> call(CheckPointEntity checkPointEntity) {
                        return checkPointEntity.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<CheckPointEntity.ResultMessageBean>>(mContext) {
                    @Override
                    public void onNext(List<CheckPointEntity.ResultMessageBean> resultMessageBeanList) {
                        if (isEdit) {
                            for (CheckPointEntity.ResultMessageBean b : resultMessageBeanList) {
                                for (CheckNumEntity.ResultMessageBean.InfoBean info : entity.getInfo()) {
                                    if (b.getId() == info.getId()) {
                                        b.setTime(info.getInspectiontime());
                                        b.setCheck(true);
                                    }
                                }
                            }
                        }
                        mPointEntities.clear();
                        mPointEntities.addAll(resultMessageBeanList);
                        mSelCheckPointAdapter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem = menu.add("保存");
        menuItem.setShowAsAction(2);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getTitle().equals("保存")) {
            generateEntityExit();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void generateEntityExit() {

        

        int i = 0;
        List<CheckPointEntity.ResultMessageBean> beanList = new ArrayList<>();
        for (CheckPointEntity.ResultMessageBean bean : mSelCheckPointAdapter.getData()) {
            i++;
            if (bean.isCheck()) {
                if (!TextUtils.isEmpty(bean.getTime())) {
                    beanList.add(bean);
                } else {
                    ToastUtil.showToast(String.format("请给第%d个岗位设置巡检时间", i));
                    return;
                }
            }
        }
        if (!beanList.isEmpty()) {
            if (isEdit) {
                mEventApi.upPointNum( String.valueOf(entity.getId()), beanList)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                            @Override
                            public void onNext(BaseRequestResult result) {
                                EventBus.getDefault().post(new DataChangeEvent());
                                finish();
                            }
                        });
            } else {
                mEventApi.addPointNum(projectId, lineId, beanList)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                            @Override
                            public void onNext(BaseRequestResult result) {
                                EventBus.getDefault().post(new DataChangeEvent());
                                finish();
                            }
                        });
            }


        } else {
            ToastUtil.showToast("请至少选择一个巡检点");
        }

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

}
